package com.nova.edc.service.system.impl;

import com.nova.edc.dao.system.DescriptionDao;
import com.nova.edc.model.system.Description;
import com.nova.edc.model.system.Login;
import com.nova.edc.model.system.ModelConstant;
import com.nova.edc.service.system.DescriptionService;
import com.nova.frame.dao.buzz.query.Query;
import com.nova.frame.dao.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by Administrator on 2016/5/16 0016.
 */
public class DescriptionServiceImpl implements DescriptionService {

    @Autowired
    private DescriptionDao novaDescriptionDao;

    @Override
    public Description insert(Description description) {
        return novaDescriptionDao.insert(description);
    }

    @Override
    public Description getByCustomerInfoId(Integer customerInfoId, Integer scrapped) {
        Query<Description> query = novaDescriptionDao.query()
                .add(Restrictions.eq("t_description.CustomerInfoId", customerInfoId))
                .add(Restrictions.eq("t_description.Scrapped", scrapped));
        return query.get();
    }

    @Override
    public int update(Description description) {
        return novaDescriptionDao.updateNotNull(description);
    }

    @Override
    public Description getByLogin(Login login, Integer customerinfoidOfSystem) {
        Description description = null;
        if (ModelConstant.ROLE_COMMON == login.getRoletype() && getByCustomerInfoId(login.getCustomerinfoid(), ModelConstant.SCRAPPED_UNDELETE) != null) {
            description = getByCustomerInfoId(login.getCustomerinfoid(), ModelConstant.SCRAPPED_UNDELETE);
        } else if (ModelConstant.ROLE_GROUP == login.getRoletype() && getByCustomerInfoId(login.getGroupid(), ModelConstant.SCRAPPED_UNDELETE) != null) {
            description = getByCustomerInfoId(login.getGroupid(), ModelConstant.SCRAPPED_UNDELETE);
        } else {
            description = getByCustomerInfoId(customerinfoidOfSystem, ModelConstant.SCRAPPED_UNDELETE);
        }
        return description;
    }

    @Override
    public Description getByCustomerInfoId(Integer customerInfoId) {
        Query<Description> query = novaDescriptionDao.query()
                .add(Restrictions.eq("t_description.CustomerInfoId", customerInfoId));
        return query.get();
    }

    @Override
    public void getDescription(List<Login> list) {
        for (Login login : list) {
            getDescription(login);
        }
    }

    @Override
    public void getDescription(Login login) {
        int id = 0;
        if (ModelConstant.ROLE_GROUP == login.getRoletype()) {
            id = login.getGroupid();
        } else {
            id = login.getCustomerinfoid();
        }
        Description description = getByCustomerInfoId(id);
        if (description != null) {
            login.setDescriptionState(description.getScrapped());
        }
    }
}
